﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnalyseCrmMana;

namespace AnalyseCRM.InteractiveData
{
    public partial class UserManagementAjax : System.Web.UI.Page
    {
        private string _paramValue = "", _paramType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["paramvalue"] != null)
            {
                _paramValue = Server.UrlDecode(Request["paramvalue"].ToString());
            }
            if (Request["paramtype"] != null)
            {
                _paramType = Request["paramtype"].ToString();
            }
            Response.Write(buildHtml(_paramValue, _paramType));
        }

        public StringBuilder buildHtml(string prparamValue, string prparamType)
        {
            StringBuilder returnValue = null;
            AccountMana accmana = null;
            switch (prparamType)
            {
                #region InsertUsers
                case "InsertUsers":
                    returnValue = new StringBuilder();
                    string[] accpara = prparamValue.Split('|');
                    accmana = new AccountMana();
                    accmana.username = accpara[0];
                    accmana.userrelname = accpara[1];
                    accmana.password = accpara[2];
                    accmana.question = accpara[3];
                    accmana.email = accpara[4];
                    accmana.mobile = accpara[5];                                        
                    accmana.islock = "F";
                    accmana.isdelete = "F";
                    accmana.logdatetime = DateTime.Now;
                    accmana.deptment = accpara[6];
                    accmana.userjob = accpara[7];
                    if (accmana.AddNewUser(accmana) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #endregion
                case "getAccountList":
                    returnValue = new StringBuilder();
                    accmana = new AccountMana();
                    IList<AccountMana> egmanalist = accmana.getAccountList(prparamValue);
                    int egcount = accmana.getAccountListCount();
                    if (egmanalist != null && egmanalist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(egmanalist));
                        returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                case "deleteAccount":
                    returnValue = new StringBuilder();
                    accmana = new AccountMana();
                    if (accmana.DeleteUser(prparamValue) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #region 重置用户密码
                case "resetuserpass":
                    returnValue = new StringBuilder();
                    accmana = new AccountMana();
                    if (accmana.ResetAccountPassword(prparamValue) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #endregion
                #region 为用户解锁
                case "clearlockforuser":
                    returnValue = new StringBuilder();
                    accmana = new AccountMana();
                    if (accmana.OpenAndLockUser(prparamValue,"F",0) > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("ERROR");
                    }
                    break;
                #endregion
                #region 获取所有角色
                case "getAllRolesList":
                    returnValue = new StringBuilder();
                    RolesMana rolemana = new RolesMana();
                    IList<RolesMana> allrolelist = rolemana.GetAllRoleList();
                    if (allrolelist != null && allrolelist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(allrolelist));
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 根据角色获取用户
                case "getAllRolesListByrole":
                    returnValue = new StringBuilder();
                    SelectBindUsers seluser = new SelectBindUsers();
                    IList<SelectBindUsers> userslist = seluser.getAllusersByrole(prparamValue);
                    if (userslist != null && userslist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(userslist));
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 
                case "bindroleuserity":
                    returnValue = new StringBuilder();
                    RolesMana rolebind = new RolesMana();
                    string[] arrayparam = prparamValue.Split('|');
                    int recount = 0;
                    if (arrayparam[2] == "D")
                    {
                        recount = rolebind.DelRoleAndUser(arrayparam[0], arrayparam[1]);
                    }
                    else if (arrayparam[2] == "A")
                    {
                        recount = rolebind.RelRoleAndUsers(arrayparam[0], arrayparam[1]);
                    }
                    if (recount > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                default:
                    break;
            }
            return returnValue;

        }  
    }
}